package com.tinckay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tinckay.entity.po.Menu;
import com.tinckay.mapper.MenuMapper;
import com.tinckay.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tinckay.service.RoleMenuService;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Jean
 * @since 2018-10-30
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    private RoleMenuService roleMenuService;

    @Override
    public List<Menu> findUserPermissions(String userName) {
        return baseMapper.findUserPermissions(userName);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addMenu(Menu menu) {
        if (null == menu.getParentId()) {
            menu.setParentId(0L);
        }
        menu.insert();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteMenus(Long... ids) {
        baseMapper.delete(new QueryWrapper<Menu>().lambda().in(Menu::getId, ids));
        roleMenuService.deleteRoleMenusByMenuId(ids);
    }

    @Override
    public List<Menu> findUserMenus(String username) {
        return baseMapper.findUserPermissions(username);
    }

}
